package com.ruoyi.process.service.impl;

import com.ruoyi.common.utils.SecurityUtils;
import com.ruoyi.process.constant.VariablesConstants;
import com.ruoyi.process.domain.Processflow;
import com.ruoyi.process.domain.workflow.FormDTO;
import com.ruoyi.process.service.FlowServiceFactory;
import com.ruoyi.process.service.IFlowInstance;
import com.ruoyi.process.service.IProcessflowService;
import com.ruoyi.process.utils.JsonNode;
import org.camunda.bpm.engine.history.HistoricProcessInstance;
import org.camunda.bpm.engine.runtime.ProcessInstance;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.HashMap;
import java.util.Map;
@Service
public class FlowInstanceImpl extends FlowServiceFactory implements IFlowInstance {
    @Resource
    private JdbcTemplate jdbcTemplate;
    @Resource
    private IProcessflowService processflowService;

    @Override
    public String startFlow(FormDTO form) {

        // 1. 设置变量
        Map<String, Object> variables = new HashMap<>(); // 请求中的

        variables.put(VariablesConstants.INITIATOR,1);// SecurityUtils.getUserId()); // 设置额外的，便于后期处理获取
       // 2.业务数据插入,带businessKey
//        form.setProcessId(form.getProcessId());
//        form.setBusinessKey(form.getBusinessKey());
//        String businessKey ="test123";

        // 3. 启动流程
        ProcessInstance processInstance = runtimeService.startProcessInstanceByKey(form.getProcessId(), form.getBusinessKey(), variables);


        return processInstance.getProcessInstanceId();
    }

    @Override
    public String revoke(FormDTO form) {
        return null;
    }

    @Override
    public int deleteFlowInstanceByIds(Long[] ids) {
        return 0;
    }

    @Override
    public String getHistory(String instanceId, String curTaskKey,Long pfid) {
        if(instanceId == null){//流程 还没有启动
//            Processflow processflow = processflowService.selectProcessflowByPfid(pfid);
//            JsonNode jsonNode = new JsonNode(processflow.getDetail());
//            jsonNode.setStartRealName(SecurityUtils.getLoginUser().getUser().getNickName());
//            Integer flag = 2;
//            jsonNode.findNodeById(historyList, condmap, "", flag, varObj);

        }else{
//            HistoricProcessInstance processInstance = historyService.createHistoricProcessInstanceQuery().processInstanceId(instanceId).singleResult();
//            R<SysUser> user = remoteUserService.getUserByUserName(processInstance.getStartUserId());
////          判断流程 是否结束了，如果结束了，就直接结束，不需要执行这些代码
//            //历史记录
//            historyList = this.optionwidth(procInsId, pfid, processInstance.getEndTime(), taskKey);


        }
        return null;
    }
}
